%%%---------------------------------------
%%%     Middle Eeast and North Africa
%%%---------------------------------------

clear all; clc;
load dataMe3.txt;  
k=2; opt=2; nsim=100; mmax=5;  
gdp={dataMe3(:,1:13) dataMe3(:,1:13) [dataMe3(:,1:9) dataMe3(:,11:13)]}; % drop Morocco for iterate
pop={dataMe3(:,14:26) dataMe3(:,14:26) [dataMe3(:,14:22) dataMe3(:,24:26)]}; 
ter={dataMe3(:,27:39) dataMe3(:,40:52) [dataMe3(:,53:61) dataMe3(:,63:65)]};

resultsMe={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag]=prepdat3(gdp{i}, ter{i}, pop{i}, j, m);
                    [T N]=size(yy);
                    xx = tlag;
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsMe.beta{i,j,m} = bfinal1;
                    resultsMe.pval{i,j,m} = pval;
                    resultsMe.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    keep resultsMe dataMe3 k opt nsim mmax ter gdp pop i j m
                end
        end
end



%%%% Results cell arrays are as follow:

%{1,1,1} = Total terrorism/1000 level
%{1,1,2} = Total terrorism/1000 first-differenced
%{1,2,1} = Total terrorism/pop level
%{1,2,2} = Total terrorism/pop first-differenced

%{2,1,1} = Domestic terrorism/1000 level
%{2,1,2} = Domestic terrorism/1000 first-differenced
%{2,2,1} = Domestic terrorism/pop level
%{2,2,2} = Domestic terrorism/pop first-differenced

%{3,1,1} = Transnat terrorism/1000 level
%{3,1,2} = Transnat terrorism/1000 first-differenced
%{3,2,1} = Transnat terrorism/pop level
%{3,2,2} = Transnat terrorism/pop first-differenced

%%%% Reading colums and rows of results: 
%%%% Columns: 1-2=total & pval; 3-4=Domestic & pval; 5-6=Trans & pval
%%%% Rows: 1-2 = lagy, ter/1000; 3-4 = lagy, ter/POP;


%%% Terrorism Level: full results
TableLevel=[resultsMe.beta{1,1,1} resultsMe.pval{1,1,1} resultsMe.beta{2,1,1} resultsMe.pval{2,1,1} ...
    resultsMe.beta{3,1,1} resultsMe.pval{3,1,1}; resultsMe.beta{1,2,1} resultsMe.pval{1,2,1} ...
    resultsMe.beta{2,2,1} resultsMe.pval{2,2,1} resultsMe.beta{3,2,1} resultsMe.pval{3,2,1}]

%%% Terrorism first-differenced: full results
TableFD=[resultsMe.beta{1,1,2} resultsMe.pval{1,1,2} resultsMe.beta{2,1,2} resultsMe.pval{2,1,2} ...
    resultsMe.beta{3,1,2} resultsMe.pval{3,1,2}; resultsMe.beta{1,2,2} resultsMe.pval{1,2,2} ...
    resultsMe.beta{2,2,2} resultsMe.pval{2,2,2} resultsMe.beta{3,2,2} resultsMe.pval{3,2,2}]

%%% Stats: number of factors y, number of factors x, number of factors u,
%%% convergence
TableStats=[resultsMe.stat{1,1,1}; resultsMe.stat{2,1,1}; resultsMe.stat{3,1,1}; ...
    resultsMe.stat{1,2,1}; resultsMe.stat{2,2,1}; resultsMe.stat{3,2,1}; ... 
    resultsMe.stat{1,1,2}; resultsMe.stat{2,1,2}; resultsMe.stat{3,1,2}; ...
    resultsMe.stat{1,2,2}; resultsMe.stat{2,2,2}; resultsMe.stat{3,2,2}]

%%%% Table 3: only results shown in Table 3
Table3=[resultsMe.beta{1,1,1} resultsMe.beta{2,1,1} resultsMe.beta{3,1,1}; ...
    resultsMe.beta{1,2,1} resultsMe.beta{2,2,1} resultsMe.beta{3,2,1}; ... 
    resultsMe.beta{1,1,2} resultsMe.beta{2,1,2} resultsMe.beta{3,1,2}; ...
    resultsMe.beta{1,2,2} resultsMe.beta{2,2,2} resultsMe.beta{3,2,2}];
Table3=[Table3(1,1:3) Table3(2,1:3); Table3(3,1:3) Table3(4,1:3); Table3(5,1:3) ...
    Table3(6,1:3); Table3(7,1:3) Table3(8,1:3)]